﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


namespace Vanglen.YDLMSCP
{
    public partial class Detail : PhoneApplicationPage
    {
        private string strId = "";
        public Detail()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                IDictionary<string, string> parameters = this.NavigationContext.QueryString;

                if (parameters.ContainsKey("id"))
                {
                    strId = parameters["id"];
                    if (!String.IsNullOrEmpty(strId))
                    {
                        BindDataById(strId);
                    }
                }

                UpdateHistory(strId);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {

                ApplicationBarIconButton btn = sender as ApplicationBarIconButton;

                SQLiteHelper helper = new SQLiteHelper();
                string sql = String.Format("update caipu set favorite=1 where _id = {0} ", strId);

                if (btn.Text == "取消收藏")
                    sql = String.Format("update caipu set favorite=0 where _id = {0} ", strId);

                int result = helper.ExecuteNonQuery(sql);

                CheckFavorite(strId);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BindDataById(string id)
        {
            SQLiteHelper helper = new SQLiteHelper();
            string sql = String.Format("select * from caipu where _id = {0} ", id);
            var result = helper.ExecuteQuery<Caipu>(sql);
            Caipu cp = result.ToList()[0] as Caipu;

            this.PageTitle.Text = cp.name;

            string strContent = "<html><head><title></title></head><body style=\"color:#ffffff; background:#000000;\">";
            string strFooter = "</body></html>";
            this.webBrowser1.NavigateToString(Common.ConvertExtendedASCII(strContent + cp.content + strFooter));

            ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            if (cp.favorite > 0)
            {
                btn.Text = "取消收藏";
                btn.IconUri = new Uri("/img/dark/appbar.favs.rest.png", UriKind.Relative);
            }
            else
            {
                btn.Text = "收藏";
                btn.IconUri = new Uri("/img/dark/appbar.favs.addto.rest.png", UriKind.Relative);
            }
        }

        private void UpdateHistory(string id)
        {
            SQLiteHelper helper = new SQLiteHelper();
            string sql = String.Format("update caipu set history=1 where _id = {0} ", strId);
            int result = helper.ExecuteNonQuery(sql);
        }

        private void CheckFavorite(string id)
        {
            SQLiteHelper helper = new SQLiteHelper();
            string sql = String.Format("select * from caipu where _id = {0} ", id);
            var result = helper.ExecuteQuery<Caipu>(sql);
            Caipu cp = result.ToList()[0] as Caipu;

            ApplicationBarIconButton btn = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            if (cp.favorite > 0)
            {
                btn.Text = "取消收藏";
                btn.IconUri = new Uri("/img/dark/appbar.favs.rest.png", UriKind.Relative);
            }
            else
            {
                btn.Text = "收藏";
                btn.IconUri = new Uri("/img/dark/appbar.favs.addto.rest.png", UriKind.Relative);
            }
        }
    }
}